package com.pr.makfilm.admin.pages.user.list;

import com.pr.makfilm.admin.api.AdminApiMappings;
import com.pr.makfilm.admin.pages.NewAbstractAdminPage;
import com.pr.makfilm.admin.pages.user.list.cmp.UserWindow;
import com.pr.makfilm.core.ajax.ConfirmationCallListener;
import com.pr.makfilm.core.cmp.authorization.UserAuthorities;
import com.pr.makfilm.core.cmp.button.BootstrapLink;
import com.pr.makfilm.core.cmp.notify.Notification;
import com.pr.makfilm.core.cmp.table.AjaxDataTable;
import com.pr.makfilm.core.cmp.table.DataProvider;
import com.pr.makfilm.core.cmp.table.SelectionChangeCallback;
import com.pr.makfilm.core.cmp.table.column.TranslatedColumn;
import com.pr.makfilm.domain.User;
import com.pr.makfilm.domain.filter.OpenSearchDescription;
import com.pr.makfilm.domain.filter.UserFilter;
import com.pr.makfilm.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.attributes.AjaxRequestAttributes;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.authroles.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.wicketstuff.annotation.mount.MountPath;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Pawel
 * Date: 08.09.13
 * Time: 21:01
 * To change this template use File | Settings | File Templates.
 */
@MountPath(AdminApiMappings.USER_LIST_PAGE)
@AuthorizeInstantiation(UserAuthorities.ADMIN)
public class UserListPage extends NewAbstractAdminPage
{
   @SpringBean
   private IUserService userService;

   private User selected;

   private AjaxDataTable table;

   private UserWindow window;

   private AjaxLink<Void> newUser;

   private BootstrapLink editUser;

   private BootstrapLink profileUser;

   private BootstrapLink deleteUser;

   public UserListPage()
   {
      super();
   }

   @Override
   protected void onInitialize()
   {
      super.onInitialize();

      add(window = new UserWindow("window")
      {
         @Override
         protected void onAfterSubmit(AjaxRequestTarget target)
         {
            refreshTableView(target);
         }
      });

      add(newUser = new AjaxLink<Void>("new")
      {
         @Override
         public void onClick(AjaxRequestTarget target)
         {
            window.resetState(true);
            window.show(target);
         }
      });
      add(editUser = new BootstrapLink("edit")
      {
         @Override
         public void onClick(AjaxRequestTarget target)
         {
            window.resetState(false);
            window.setUser(selected);
            window.show(target);
         }

         @Override
         protected void onConfigure()
         {
            setEnabled(selected != null);
         }
      });

      add(deleteUser = new BootstrapLink("delete")
      {
         @Override
         public void onClick(AjaxRequestTarget target)
         {
            userService.delete(selected);
            Notification.success(getString("user.successfully.deleted"));
            refreshTableView(target);
         }

         @Override
         protected void updateAjaxAttributes(AjaxRequestAttributes attributes)
         {
            super.updateAjaxAttributes(attributes);
            attributes.getAjaxCallListeners().add(new ConfirmationCallListener(getString("user.delete.confirm")));
         }

         @Override
         protected void onConfigure()
         {
            setEnabled(selected != null);
         }
      });

      add(profileUser = new BootstrapLink("profile")
      {
         @Override
         public void onClick(AjaxRequestTarget target)
         {

         }

         @Override
         protected void onConfigure()
         {
            setEnabled(selected != null);
         }
      });

      List<IColumn<User, String>> columns = new ArrayList<IColumn<User, String>>();
      columns.add(new PropertyColumn<User, String>(
              new ResourceModel("user.first.name"),
              User.FIELD_FIRST_NAME,
              User.FIELD_FIRST_NAME
      ));
      columns.add(new PropertyColumn<User, String>(
              new ResourceModel("user.last.name"),
              User.FIELD_LAST_NAME,
              User.FIELD_LAST_NAME
      ));
      columns.add(new PropertyColumn<User, String>(
              new ResourceModel("user.login"),
              User.FIELD_LOGIN,
              User.FIELD_LOGIN
      ));
      columns.add(new PropertyColumn<User, String>(
              new ResourceModel("user.email"),
              User.FIELD_EMAIL,
              User.FIELD_EMAIL
      ));
      columns.add(new TranslatedColumn<User, String>(
              new ResourceModel("user.type"),
              User.FIELD_TYPE,
              User.FIELD_TYPE,
              "UserTypeEnum."
      ));
      OpenSearchDescription<User> osd = new OpenSearchDescription<User>();
      UserFilter filter = new UserFilter();
      osd.setFilter(filter);
      DataProvider dataProvider = new DataProvider(userService, osd);
      table = new AjaxDataTable<User>("table", columns, dataProvider);
      table.setSelectionChangeCallback(new SelectionChangeCallback<User>()
      {
         @Override
         public void onSelectionChange(AjaxRequestTarget target, IModel<User> model)
         {
            selected = model.getObject();
            refreshTableView(target);
         }
      });
      add(table);
   }

   @Override
   protected Label providePageTitleLabel(String id)
   {
      return new Label(id, new ResourceModel("page.title"));
   }

   private void refreshTableView(AjaxRequestTarget target)
   {
      target.add(table);
      target.add(editUser);
      target.add(profileUser);
      target.add(deleteUser);
   }
}
